package com.bestcem.xm.tinyurl.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bestcem.xm.component.security.annotation.AuthIgnore;
import com.bestcem.xm.componet.tinyurl.service.TinyurlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "短链接服务")
@RestController
@RequiredArgsConstructor
@AuthIgnore
public class TinyUrlController {

    private @NonNull TinyurlService tinyurlService;

    @ApiOperation(value = "长链接转短链接")
    @PostMapping(value = {"/api/tinyurl/shorten/"})
    public Map<String, Object> postShorten(@RequestBody JSONObject param) {
        String url = param.getString("url");
        if (ObjectUtil.isEmpty(url)) {
            throw new RuntimeException("url is required");
        }
        return tinyurlService.genShortUrl(url);
    }

    @ApiOperation(value = "短链接转长链接")
    @PostMapping(value = {"/api/tinyurl/revert/"})
    public Map<String, Object> postRevert(@RequestBody JSONObject param) {
        String url = param.getString("url");
        if (ObjectUtil.isEmpty(url)) {
            throw new RuntimeException("url is required");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("short_url", url);
        map.put("long_url", tinyurlService.toRealUrl(url.substring(url.lastIndexOf("/") + 1)));
        return map;
    }

    @ApiOperation(value = "长链接转短链接-列表")
    @PostMapping(value = {"/api/tinyurls/shorten/"})
    public Map<String, Object> postShortens(@RequestBody JSONObject param) {
        JSONArray urls = param.getJSONArray("urls");
        if (ObjectUtil.isEmpty(urls)) {
            throw new RuntimeException("urls is required");
        }
        List<Object> result = new ArrayList<>();
        urls.forEach(a -> result.add(tinyurlService.genShortUrl((String) a)));
        Map<String, Object> map = new HashMap<>();
        map.put("result", result);
        map.put("code", 0);
        return map;
    }

    @ApiOperation(value = "短链接转长链接-列表")
    @PostMapping(value = {"/api/tinyurls/revert/"})
    public Map<String, Object> postReverts(@RequestBody JSONObject param) {
        JSONArray urls = param.getJSONArray("urls");
        if (ObjectUtil.isEmpty(urls)) {
            throw new RuntimeException("url is required");
        }
        List<Object> result = new ArrayList<>();
        urls.forEach(a -> {
            Map<String, Object> tmp = new HashMap<>();
            String longUrl = tinyurlService.toRealUrl(((String) a).substring(((String) a).lastIndexOf("/") + 1));
            if ("".equals(longUrl)) {
                tmp.put("code", 236);
                tmp.put("msg", "not such short url");
            } else {
                tmp.put("long_url", longUrl);
            }
            tmp.put("short_url", a);

            result.add(tmp);
        });
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("result", result);
        return map;
    }

    @ApiOperation(value = "短链接重定向")
    @GetMapping(value = {"/t/{keyid:[a-zA-Z0-9]{6}}"})
    public void redirectUrl(@PathVariable(value = "keyid") String keyid,
                            HttpServletResponse response) throws IOException {
        response.sendRedirect(tinyurlService.toRealUrl(keyid));
    }

}